// pages/ PublishProblem/index.js

import {
  HTTP
} from '../../../utils/http.js'
let Myhttp = new HTTP()
// import
// Validate
// from '../../../validate.js'
// let validate = new Validate();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    checked: false,
    is_return: false,
    title: '',
    desc: '',
    phone: '',
    num: '',
    money: '',
    visit: '',
    visit_cursor: 0,
    imgfile: '',
    qid: '',
    cover: ''
  },
  title: function(e) {
    this.setData({
      title: e.detail.value
    })
  },
  describe: function(e) {
    this.setData({
      desc: e.detail.value
    })
  },
  phone: function(e) {
    this.setData({
      phone: e.detail.value
    })
  },
  num: function(e) {
    this.setData({
      num: e.detail.value
    })
  },
  money: function(e) {
    this.setData({
      money: e.detail.value
    })
  },
  visit: function(e) {
    this.setData({
      visit: e.detail.value,
      visit_cursor: e.detail.cursor
    })
    // console.log(e)
  },
  // imgclick: function() {
  //   let that = this
  //   let id = wx.getStorageSync('id')
  //   wx.chooseImage({
  //     success: function(res) {
  //       let tempFilePaths = res.tempFilePaths

  //       wx.uploadFile({
  //         url: 'http://192.168.0.218/questionnaire/php/questionnaire/public/index.php/questionnaire/index/uploadPicture',
  //         filePath: tempFilePaths[0],
  //         name: 'file',
  //         formData: {
  //           'unid': id
  //         },
  //         success: function(res) {
  //           let aa = JSON.parse(res.data)
  //           //console.log(aa.data.url_domin + aa.data.file )
  //           that.setData({
  //             cover: aa.data.url_domin + aa.data.file,
  //             imgfile: aa.data.file
  //           })
  //           //console.log(that.data)
  //           //do something
  //         }
  //       })
  //     }
  //   })

  // },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },
  switchChange: function(e) {
    this.setData({
      checked: e.detail.value
    })
  },
  switch2Change: function(e) {

    this.setData({
      is_return: e.detail.value
    })
  },
  //下一步
  publish1: function() {
    let that = this
    //console.log(JSON.stringify(this.data))
    let index
    if (this.data.is_return) {
      index = 1
    } else {
      index = 0
    }

    // console.log(this.data.cover)
    let myreg = /^[1][3,4,5,7,8][0-9]{9}$/;
    if (this.data.title == '') {
      wx.showToast({
        title: '请输入标题'
      })
    } else if (this.data.desc == '') {
      wx.showToast({
        title: '请输入描述'
      })
    } else if (!myreg.test(this.data.phone)) {
      wx.showToast({
        title: '请输入正确电话号码'
      })
    } else if (this.data.num == '') {
      wx.showToast({
        title: '请输入份数'
      })
    }else {
      Myhttp.request({
        url: 'questionnaire/questionnaire/addQuestionnaire',
        data: {
          title: that.data.title,
          desc: that.data.desc,
          phone: that.data.phone,
          num: that.data.num,
          cover: that.data.imgfile,
          price: that.data.money,
          is_return: index,
          return_desc: that.data.visit,
        },
        success: (res) => {
          //  console.log('下一步')
           console.log(res)
          if (res.data.code == '200') {
            Myhttp.jump({
              method:'reLaunch',
              url: `/pages/PublishQuestionnaire/EditProblem/index?qid=${res.data.data.qid}`
            })
          } else if (res.data.code == '400'){
            wx.showToast({
              title: res.data.msg,
              icon: "none"
            })
          }
        }
      })
    }



  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    this.setData({
      checked: false,
      is_return: false,
      title: '',
      desc: '',
      phone: '',
      num: '',
      money: '',
      visit: '',
      visit_cursor: 0,
      imgfile: '',
      qid: '',
      cover: ''
    })
    wx.stopPullDownRefresh() //停止下拉刷新
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    return {
      path: '/pages/PublishQuestionnaire/PublishProblem/index'
    }
  }
})